package com.notebad.ui;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.notebad.R;

public class AudioView extends Activity implements OnClickListener {

	private static final String TAG = "[AudioView]";
	private ImageButton record;
	private ImageButton stop;
	private ImageButton play;
	private ImageButton pause;
	private TextView time;
	private ProgressBar progressBar;
	private int progressStatus = 0;
	
	private Handler handler = new Handler();

	private MediaRecorder recorder;
	private MediaPlayer player;
	private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.3gp";


	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");

		setContentView(R.layout.audio);		
		record = (ImageButton) findViewById(R.id.Record);
		record.setOnClickListener(this);
		stop = (ImageButton) findViewById(R.id.Stop);
		stop.setOnClickListener(this);
		play = (ImageButton) findViewById(R.id.Play);
		play.setOnClickListener(this);
		pause = (ImageButton) findViewById(R.id.Pause);
		pause.setOnClickListener(this);
		time = (TextView) findViewById(R.id.Time);
		progressBar = (ProgressBar) findViewById(R.id.ProgressBar);
	}


	/**
	 * Starts a new recording.
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	public void record() throws IllegalStateException, IOException
	{
		String pathRecord = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.3gp";

		/*
		 * If the recorder already exists
		 */
		if (recorder != null) 
		{
			recorder.start();
			return;
		}

		String state = android.os.Environment.getExternalStorageState();
		if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  
		{
			throw new IOException("SD Card is not mounted.  It is " + state + ".");
		}

		// Make sure the directory we plan to store the recording in exists
		File directory = new File(pathRecord).getParentFile();
		if (!directory.exists() && !directory.mkdirs()) 
		{
			throw new IOException("Path to file could not be created.");
		}

		recorder = new MediaRecorder();
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setOutputFile(pathRecord);
		recorder.prepare();
		recorder.start();

	}

	/**
	 * Stops a recording that has been previously started.
	 */
	public void stop()
	{
		recorder.stop();
		recorder.release();
	}

	public void play() throws IllegalArgumentException, IOException
	{
		/*
		 * If the player already exists
		 */
		if (player != null) 
		{
			player.start();
			return;
		}

		player = new MediaPlayer();
		Log.v(TAG, "Path: "+path);
		player.setDataSource(path);
		player.prepare();
		Log.v(TAG, "Duration: " + player.getDuration());
		//time.setText(player.getCurrentPosition());
		player.setLooping(true);
		progressBar.setProgress(0);
		progressBar.setMax(player.getDuration());
		player.start();
		onEverySecond.run();
		/*new Thread(new Runnable() 
		{
			public void run() 
			{
				while (progressStatus < 100) 
				{
					progressStatus = player.getCurrentPosition();

					// Update the progress bar
					handler.post(new Runnable() 
					{
						public void run() 
						{
							progressBar.setProgress(progressStatus);
						}
					});
				}
			}
		}).start();*/
		

	}
	
	public void pause()
	{
		if (player != null) 
		{
			player.pause();
		}
	}
	
	private Runnable onEverySecond = new Runnable() 
	{
		public void run() 
		{
			if (player!=null) 
			{
				progressBar.setProgress(player.getCurrentPosition());
			}
		}
	};

	@Override
	public void onClick(View view) 
	{
		switch(view.getId()) 
		{
		case R.id.Record:
			Log.d(TAG, "Record");
			try 
			{
				this.record();
			} 
			catch (IllegalStateException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			return;

		case R.id.Stop:
			Log.d(TAG, "Stop");
			this.stop();
			return;

		case R.id.Play:
			Log.d(TAG, "Play");
			try 
			{
				this.play();
			} 
			catch (IllegalArgumentException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return;

		case R.id.Pause:
			Log.d(TAG, "Pause");
			this.pause();
			return;
		}
	}


}
